/**
 * 企业实名认证
 * @authors supmain (mtingfeng@gmail.com)
 * @date    2015-03-04 14:31:59
 * @update  2015-03-04 14:31:59
 * @for webapp.html
 * @version $Id$
 */
var BSTYUN = BSTYUN || {};
BSTYUN.company_authenticate = {
	init: function(){
		var that = this;
		//上传图片
		that.uploaderImg();
		// 日期输入
		that.adjustDateType();
		that.submitForm();
	},
	uploaderImg: function(){
		var $uploader = $(".uploader");
		$uploader.on("change","input[type=file]",function(){
			var $this = $(this),
				value = $this.val();
			if(!/.(gif|bmp|jpg|jpeg|png|JPG|PNG)$/.test( value )) {
			    BSTYUN.dialog.warn("图片类型必须是.gif.bmp.jpeg,jpg,png中的一种");
			    return false;
			};
			var fileElementId = $this[0].id;
			$.ajaxFileUpload({
				url:"/uploadFile/image?attachType=3&objectName=certification",
				secureuri: false,
				fileElementId: fileElementId,
				dataType: "json",
				success:function( data ) {
					if(data.message =="上传文件成功"){
						$('#'+fileElementId+'_id').val( data.attachId);
						$('#'+fileElementId+'_name').text( data.fileName);
					}else{
						BSTYUN.dialog.warn(data.message);
					}
					
				},
				error:function (data, status, e) {
					BSTYUN.dialog.warn("图片上传失败,请重新选择图片！");
				}
			});
		});
	},
	adjustDateType: function(){
		var $business_date = $(".business_date"),
			$inputs = $business_date.find("input"),
			$start = $inputs.filter("input[name=business_stime]"),
			$end = $inputs.filter("input[name=business_etime]") ,
			$type = $inputs.filter("input[name=business_type]") ;
		$business_date.on("click","input[name=business_type]",function(event){
			var $this = $(this);
			if( $this.prop("checked") ){
				$this.prop("checked" , true );
				$end.val("").addClass("disabled");
			}else{
				$this.prop("checked" , false );
				$end.removeClass("disabled");
			};
		}).on("focus","input[name=business_etime]",function(){
			$(this).removeClass("disabled");
			$type.prop("checked" , false );
		});
	},
	submitForm: function(){
		var $sumbitbox = $(".sumbitbox") ,
			$form_field = $(".form_field") ,
			$inputs = $form_field.find("input") ,
			$company_name = $inputs.filter("input[name=company_name]") ,
			$license_code = $inputs.filter("input[name=license_code]") ,
			$company_address = $inputs.filter("input[name=company_address]") ,
			$business_stime = $inputs.filter("input[name=business_stime]") ,
			$business_etime = $inputs.filter("input[name=business_etime]") ,
			$business_type = $inputs.filter("input[name=business_type]") ,
			$common_address = $inputs.filter("input[name=common_address]") ,
			$provinceCode = $("select[name=provinceCode]") ,
			$cityCode = $("select[name=cityCode]") ,
			$areaCode = $("select[name=areaCode]") ,
			$phone_num = $inputs.filter("input[name=phone_num]") ,
			$auth_license = $inputs.filter("#auth_license_id") ,
			$duplicate_license = $inputs.filter("#duplicate_license_id") ,
			$organization_code = $inputs.filter("input[name=organization_code]") ,
			$business_range = $inputs.filter("input[name=business_range]") ,
			$start_fund = $inputs.filter("input[name=start_fund]") ,
			$fax = $inputs.filter("input[name=fax]") ,
			$authcode = $inputs.filter("input[name=authcode]") ,
			$id = $("#id"),
			$state = $("#state");
		var reg_date = /((^((1[8-9]\d{2})|([2-9]\d{3}))(-)(10|12|0?[13578])(-)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(11|0?[469])(-)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(0?2)(-)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)(-)(0?2)(-)(29)$)|(^([3579][26]00)(-)(0?2)(-)(29)$)|(^([1][89][0][48])(-)(0?2)(-)(29)$)|(^([2-9][0-9][0][48])(-)(0?2)(-)(29)$)|(^([1][89][2468][048])(-)(0?2)(-)(29)$)|(^([2-9][0-9][2468][048])(-)(0?2)(-)(29)$)|(^([1][89][13579][26])(-)(0?2)(-)(29)$)|(^([2-9][0-9][13579][26])(-)(0?2)(-)(29)$))/i ,
			reg_telphone = /^0\d{2,3}-\d{5,9}$/ ,
			reg_num = /^[0-9]\d*$/ ,
			reg_fax = /^0\d{2,3}-\d{5,9}$/ ;
		
		// 获取数据
		function getData(){
			var id = $.trim( $id.val() ),
				state = $.trim( $state.val() );
			if(state != 2) id = '0';
			if(state == "" || state == 2) state = 1;
			return {
				company_name: $.trim( $company_name.val() ) ,
				license_code: $.trim( $license_code.val() ) ,
				company_address: $.trim( $company_address.val() ) ,
				bnessLicenseProvince : $.trim( $provinceCode.val() ) ,
				bnessLicenseCity : $.trim( $cityCode.val() ) ,
				bnessLicenseArea : $.trim( $areaCode.val() ) ,
				business_stime: $.trim( $business_stime.val() ) ,
				business_etime: $.trim( $business_etime.val() ) ,
				common_address: $.trim( $common_address.val() ) ,
				phone_num: $.trim( $phone_num.val() ) ,
				auth_license: $.trim( $auth_license.val() ) ,
				duplicate_license: $.trim( $duplicate_license.val() ) ,
				organization_code: $.trim( $organization_code.val() ) ,
				business_range: $.trim( $business_range.val() ) ,
				start_fund: $.trim( $start_fund.val() ) ,
				fax: $.trim( $fax.val() ) ,
				id : id,
				state : state ,
				fax: $.trim( $fax.val() )
			};
		};
		// 校验数据
		function checkData( data ){
			if( data.company_name == "" ){
				BSTYUN.dialog.warn("企业名称不能为空");
				return false;
			};
			if( data.license_code == "" ){
				BSTYUN.dialog.warn("企业执照注册号不能为空");
				return false;
			};
			// 企业执照所在地校验
			if( data.bnessLicenseProvince == "" ){
				BSTYUN.dialog.warn("企业执照所在地省份不能为空");
				return false;
			};
			// 营业期限校验
			if( data.business_stime == "" ){
				BSTYUN.dialog.warn("企业营业期限起始时间不能为空");
				return false;
			};
			if( !reg_date.test( data.business_stime ) ){
				BSTYUN.dialog.warn("企业营业期限起始时间格式不正确");
				return false;
			};
			if( (new Date( data.business_stime.replace(/-/g,'/') )).getTime() > (new Date()).getTime()  ){
				BSTYUN.dialog.warn("企业营业期限起始时间不能大于当前时间");
				return false;
			};
			if( $business_type.prop("checked") ){
				data.business_etime = 'longtime';
			}else{
				if( data.business_etime == "" ){
					BSTYUN.dialog.warn("企业营业期限结束时间不能为空");
					return false;
				};
				if( !reg_date.test( data.business_etime ) ){
					BSTYUN.dialog.warn("企业营业期限结束时间格式不正确");
					return false;
				};
				if( (new Date( data.business_stime.replace(/-/g,'/') )).getTime() > (new Date( data.business_etime.replace(/-/g,'/') )).getTime()  ){
					BSTYUN.dialog.warn("企业营业期限起始时间不能大于结束时间");
					return false;
				};
			};
			// 常用地址校验
			if( data.common_address == "" ){
				BSTYUN.dialog.warn("常用地址不能为空");
				return false;
			};
			if( data.phone_num == "" ){
				BSTYUN.dialog.warn("联系电话不能为空");
				return false;
			};
			if( !reg_telphone.test( data.phone_num ) ){
				BSTYUN.dialog.warn("联系电话格式不正确");
				return false;
			};
			if( data.auth_license == "" ){
				BSTYUN.dialog.warn("请上传营业执照副本扫描件");
				return false;
			};
			if( data.start_fund != "" ){
				if( !reg_num.test( data.start_fund ) ){
					BSTYUN.dialog.warn("注册资金格式不正确");
					return false;
				};
			};
			if( data.fax != "" ){
				if( !reg_fax.test( data.fax ) ){
					BSTYUN.dialog.warn("传真格式不正确");
					return false;
				};
			};
			return true;
		};
		$sumbitbox.on("click",".btn",function(){
			if( checkData( getData() ) ){
				$.ajax({
					url: '/user/companyCertificate',
					type: 'post',
					dataType: 'json',
					data: getData(),
					beforeSend: null ,
					complete: null ,
					success: function(data){
						if(data.isSuccess){
							// 提示提交成功
							$(".form_field").hide();
							$(".uc_content").html("<p>信息提交成功，请耐心等待审核！</p>")
						}else{
							BSTYUN.dialog.warn("提交失败！请重新提交！");
						}
						
					},
					error:function(){
						BSTYUN.dialog.warn("网络异常！请重新提交！");
					}
				});
			};
		});
		
	}
};
$(function(){
	BSTYUN.company_authenticate.init();
});
